package net.togogo.web.controller;

import net.togogo.untils.ImageCode;
import net.togogo.untils.ImageConstants;
import net.togogo.untils.ImageCodeGenerator;
import net.togogo.web.entity.User;
import net.togogo.web.service.UserService;
import org.springframework.social.connect.web.HttpSessionSessionStrategy;
import org.springframework.social.connect.web.SessionStrategy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.ServletWebRequest;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class UserController {

    private SessionStrategy sessionStrategy = new HttpSessionSessionStrategy();

    @Resource
    UserService userService;

    @RequestMapping("/toLogin")
    public String login(String user_login, String password_login ){
        User user = userService.Login(user_login, password_login);
        System.out.println("user = " + user);
        return "index";
    }

    //显示验证码
    @RequestMapping("/code/image")
    public void getCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ImageCode imageCode = ImageCodeGenerator.createImageCode();
        sessionStrategy.setAttribute(new ServletWebRequest(request),ImageConstants.SESSION_KEY_CODE,imageCode);
        ImageIO.write(imageCode.getImage(),ImageConstants.CODE_TYPE,response.getOutputStream());
    }
}
